﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;


public partial class Buyer_Action : System.Web.UI.Page
{

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["at"].ToString() == "1")
        {
            pnladdnote.Visible = true;
        }
        if (Request.QueryString["at"].ToString() == "2")
        {
            pnlbiddecline.Visible = true;
        }
        if (Request.QueryString["at"].ToString() == "0")
        {
            pnlbidselect.Visible = true;
        }
    }

    #endregion

    #region Save Notes Click

    protected void bttnsavenote_Click(object sender, EventArgs e)
    {
        Bid bidObj = Bid.GetBidByBidID(Int32.Parse(Request.QueryString["BidID"].ToString()));
        bidObj.BuyerNote = txtnotes.Text;
        Bid.Update(bidObj);

        pnladdnote.Visible = false;
        pnlfeedback.Visible = true;
        lblfeedback.Text = "Your Notes have been saved with respect to this Bid...";

        //String strscriptcc = "<script language=javascript>ClientClose();</script>";
        //Page.RegisterStartupScript("clientScript", strscriptcc);


        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);
        
        
        

    }
    
    #endregion

    #region Cancel Note Click

    protected void bttncancelnote_Click(object sender, EventArgs e)
    {
        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);
    }

    #endregion

    #region Decline Button Click

    protected void bttnbiddecline_Click(object sender, EventArgs e)
    {

        Bid bidObj = Bid.GetBidByBidID(Int32.Parse(Request.QueryString["BidID"].ToString()));

        // send email to the contractor
        string emailfeedback;
        bool emailsend = ConstructionBidOnline.MailUtils.SendEmail(RegisteredUser.GetRegisteredUserByID(bidObj.CompanyID_RKRegisteredUser).EmailAddress, RegisteredUser.GetRegisteredUserByID(bidObj.CompanyID_RKRegisteredUser).FirstName, "Proposal Declined...", "Buyer has declined your bid for the Job (JobID=" + bidObj.JobID_RKJobs + ").<br/><br/>The reason why your proposal wasn’t accepted <br/><br/> " + txtdeclinereason.Text + " <br/><br/> This is an system generated mail so please do not reply to this mail.", out emailfeedback);
            

        bidObj.BidStatus = "Declined";
        bidObj.AwardedOrDeclineDate = DateTime.Now;
        bidObj.Reason = txtdeclinereason.Text;
        Bid.Update(bidObj);

        //Notification
        Notifications notificationObj = Notifications.CreateNew();
        notificationObj.UserID = Int32.Parse(Session["userid"].ToString());
        notificationObj.ActionDone = " has declined your bid";
        notificationObj.NotificationDate = DateTime.Now;
        notificationObj.NotificationReference = bidObj.JobID_RKJobs;
        Notifications.Update(notificationObj);

        pnlbiddecline.Visible = false;
        pnlfeedback.Visible = true;
        lblfeedback.Text = "Proposal Declined Successfully";

        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);
        
    }

    #endregion

    #region Decline Cancel Click

    protected void bttncanceldecline_Click(object sender, EventArgs e)
    {
        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);
    }

    #endregion

    #region Button Select Yes Click

    protected void bttnselectyes_Click(object sender, EventArgs e)
    {
        bool bidalreadyselected = false;

        Job jobObj = Job.GetJobByID(Session["jobid"].ToString());
        
        BidCollection bidsObj = Bid.GetAllBidsByJobID(jobObj.JobID, true);

        foreach (Bid bid in bidsObj)
        {
            if ((bid.BidStatus == "Selected") || (bid.BidStatus == "Work in progress"))
                bidalreadyselected = true;
        }

        if (bidalreadyselected == false)
        {
            Bid bidObj = Bid.GetBidByBidID(Int32.Parse(Request.QueryString["BidID"].ToString()));
            

            // send email to the contractor
            string emailfeedback;
            bool emailsend = ConstructionBidOnline.MailUtils.SendEmail(RegisteredUser.GetRegisteredUserByID(bidObj.CompanyID_RKRegisteredUser).EmailAddress, RegisteredUser.GetRegisteredUserByID(bidObj.CompanyID_RKRegisteredUser).FirstName, " Congratulations Proposal Selected...", RegisteredUser.GetRegisteredUserByID(Int32.Parse(Session["userid"].ToString())).FirstName + " " + RegisteredUser.GetRegisteredUserByID(Int32.Parse(Session["userid"].ToString())).LastName + " has selected your bid for the proposed Job (JobID=" + bidObj.JobID_RKJobs + ").<br/>and he is ready to start working with you on the terms you summited.If you are in agreement and would like to start the contracted job<br/>click the link below.<br/><br/>http://www.constructionbidonline.com/JobAcceptence.aspx?action=accept&bidid=" + bidObj.BidID + "&jobid=" + bidObj.JobID_RKJobs + "<br/><br/>and to reject click here<br/><br/><br/>http://www.constructionbidonline.com/JobAcceptence.aspx?action=reject&bidid=" + bidObj.BidID + "&jobid=" + bidObj.JobID_RKJobs + "<br/><br/> This is an system generated mail so please do not reply to this mail.", out emailfeedback);



            bidObj.BidStatus = "Selected";
            bidObj.AwardedOrDeclineDate = DateTime.Now;
            bidObj.Reason = "";
            Bid.Update(bidObj);


            //Job History
            JobHistory jobhistoryobj = JobHistory.CreateNew();
            jobhistoryobj.JobID = Session["jobid"].ToString();
            jobhistoryobj.ActionPerformed = "You selected a proposal for this job.";
            JobHistory.Update(jobhistoryobj);


            //Notification
            Notifications notificationObj = Notifications.CreateNew();
            notificationObj.UserID = Int32.Parse(Session["userid"].ToString());
            notificationObj.ActionDone = " has selected your bid";
            notificationObj.NotificationDate = DateTime.Now;
            notificationObj.NotificationReference = bidObj.BidID.ToString();
            notificationObj.LinkedJobID = jobObj.JobID;
            Notifications.Update(notificationObj);


            pnlbidselect.Visible = false;
            pnlfeedback.Visible = true;
            lblfeedback.Text = "Proposal Selected Successfully";


            String strscript = "<script language=javascript>refreshandclose();</script>";
            if (!Page.IsStartupScriptRegistered("clientScript"))
                Page.RegisterStartupScript("clientScript", strscript);
        }
        else
        {
            pnlbidselect.Visible = false;
            pnlfeedback.Visible = true;
            lblfeedback.Text = "You have already selected a proposal with respect to this job, so you can not select any another proposal with respect to this job.";

            String strscript = "<script language=javascript>refreshandclose();</script>";
            if (!Page.IsStartupScriptRegistered("clientScript"))
                Page.RegisterStartupScript("clientScript", strscript);

        }
    }


    #endregion

    #region Button Select No Click

    protected void bttnselectno_Click(object sender, EventArgs e)
    {
        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);
    }

    #endregion

    #endregion

}
